﻿using System;
using System.Collections.Specialized;
using System.Windows;

namespace Gmantis.Controls.DataGrid
{
    internal class DataGridRowsVariableScrollStrategy : DataGridVariableScrollStrategy
    {
        // Methods
        public DataGridRowsVariableScrollStrategy(DataGridView view)
        {
            EventHandler<DataGridRowEventArgs> handler = null;
            NotifyCollectionChangedEventHandler handler2 = null;
            EventHandler<DataGridRowsAddedEventArgs> handler3 = null;
            EventHandler<DataGridRowsDeletedEventArgs> handler4 = null;
            EventHandler<DataGridRowEventArgs> handler5 = null;
            EventHandler<DataGridRowEventArgs> handler6 = null;
            EventHandler<PropertyChangedEventArgs<int>> handler7 = null;
            EventHandler<PropertyChangedEventArgs<int>> handler8 = null;
            this.View = view;
            if (handler == null)
            {
                handler = delegate(object s, DataGridRowEventArgs e)
                {
                    ViewRow viewRow = e.Row.ViewRow;
                    this.UpdateViewRow(viewRow);
                };
            }
            this.View.RowActualHeightChanged += handler;
            if (handler2 == null)
            {
                handler2 = (s, e) => this.DestroyTable();
            }
            this.View.Model.Rows.CollectionChanged += handler2;
            if (handler3 == null)
            {
                handler3 = (s, e) => this.DestroyTable();
            }
            this.View.Model.RowsAdded += handler3;
            if (handler4 == null)
            {
                handler4 = (s, e) => this.DestroyTable();
            }
            this.View.Model.RowsDeleted += handler4;
            if (handler5 == null)
            {
                handler5 = (s, e) => this.DestroyTable();
            }
            this.View.Model.GroupRowVisibilityChanged += handler5;
            if (handler6 == null)
            {
                handler6 = (s, e) => this.DestroyTable();
            }
            this.View.Model.RowVisibilityChanged += handler6;
            if (handler7 == null)
            {
                handler7 = (s, e) => this.DestroyTable();
            }
            this.View.DataGrid.FrozenBottomRowsCountChanged += handler7;
            if (handler8 == null)
            {
                handler8 = (s, e) => this.DestroyTable();
            }
            this.View.DataGrid.FrozenTopRowsCountChanged += handler8;
        }

        protected override AggregatingTable<double, DoubleSumAggregator> BuildTable()
        {
            double[] lengths = new double[this.View.Model.Rows.Count];
            double num = this.View.DataGrid.RowHeight.IsAbsolute ? this.View.DataGrid.RowHeight.Value : 24.0;
            for (int i = 0; i < this.View.Model.Rows.Count; i++)
            {
                lengths[i] = num;
            }
            foreach (DataGridRow row in this.View.Model.Rows.AsEnumerable(null, null, false))
            {
                int index = row.Index;
                double totalRenderHeight = num;
                if (row.IsFrozen || !row.IsVisible(false))
                {
                    totalRenderHeight = 0.0;
                }
                else
                {
                    ViewRow viewRow = row.GetViewRow(false);
                    if ((viewRow != null) && viewRow.DesiredHeightIsSet)
                    {
                        totalRenderHeight = viewRow.GetTotalRenderHeight();
                    }
                }
                lengths[index] = totalRenderHeight;
                DataGridGroupRow row3 = row as DataGridGroupRow;
                if ((row3 != null) && (row3.GroupRowsVisibility == Visibility.Collapsed))
                {
                    foreach (DataGridRow row4 in row3.Rows)
                    {
                        lengths[row4.Index] = 0.0;
                    }
                }
            }
            return new AggregatingTable<double, DoubleSumAggregator>(lengths);
        }

        private void UpdateViewRow(ViewRow viewRow)
        {
            if (viewRow.Row.Index != -1)
            {
                base.Table.Update(viewRow.Row.Index, viewRow.Row.IsFrozen ? 0.0 : viewRow.GetTotalRenderHeight());
                base.RaiseOnChanged();
            }
        }

        // Properties
        public DataGridView View { get; set; }
    }
}
